<?php
/*
 * Created on 7 Dec, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
/**
 * 
 * Description:
 * The class base on 'glools/loadinc' library  
 */
class Glools_Inc_Tools{
    
    static public $objLibLoadInc = array();
    
    // Only save the last module you set.
    static public $module = null;
    
    static public function loadInc($name, $type, $module){
        if(null !== $module){
            self::setModule($module);
        }
        
        self::init($module)->load($name, $type);
    }
    
    static public function loadData($name, $type, $module) {
    	if(null !== $module){
            self::setModule($module);
        }
        
        return self::init($module)->load($name, $type);
    }
    
    static public function getIncPath($name, $type, $module) {
        if(null !== $module){
            self::setModule($module);
        }
        return self::init($module)->getPath($name, $type);
    }
    
    static public function setModule($module) {
        if( !module_exists($module) ){
            throw new Exception("Error, Module '$module' does not exist.");
        }
        
        if( !isset(self::$objLibLoadInc[$module]) ){
            self::$objLibLoadInc[$module] = null;
        }
    }
    
    static protected function init($module) {
        if( null !== self::$objLibLoadInc[$module] && (self::$objLibLoadInc[$module] instanceof Lib_Glools_Loadinc) ){
            return self::$objLibLoadInc[$module];
        }
        
        self::$objLibLoadInc[$module] = Lib_Glools_Load::load('glools/loadinc', 'lib', $module);
        
        return self::$objLibLoadInc[$module];
    }
}

/**
 * parameters for $view:
 * $view = array(
 *     'view' => '[view path]'
 * );
 * */
function glools_load_view($view, $options){
	$objView = Lib_Glools_Load::load('glools/view', 'lib');
    $view = $objView->loadView($view, $options);
    
    if (!empty($view)) {
    	return drupal_render($view);
    }
    return null;
}


/**
 * This is a sample function for use view lib.
 * */
function glools_load_views($section, &$vars, $options=array(), $module=null) {
	
    if (empty($module) || !module_exists($module)) {
        throw new Exception("Provided module does not exist in system.");
    }
    
    $objView = Lib_Glools_Load::load('glools/view', 'lib', array('module'=>$module));
    
    $objView->loadPreset()->buildViews($section, $options);
    
    $objView->attachToPage($vars);
    
}